package com.pojo.payment.controller;


import com.pojo.payment.entity.Payment;
import com.pojo.payment.service.PaymentService;
import com.pojo.payment.service.ReservationService;
import com.pojo.payment.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author Evan
 */
@RestController
public class PaymentController {
    @Value("${server.port}")
    private String port;
    @Autowired
    private PaymentService paymentService;


    @Autowired
    private ReservationService reservationService;
    @Autowired
    private TicketService ticketService;

    @GetMapping(value = "/payment/queryPaymentByUserId/{id}")
    public Map<Long, Payment> queryPaymentByUserId(@PathVariable("id") Long id) {
        return this.paymentService.queryPaymentByUserId(id);
    }


    @GetMapping(value = "/payment/payTicket/{userId}/{t_id}")
    public String payTicket(@PathVariable("userId") Long userId, @PathVariable("t_id") Long t_id) {
        if (reservationService.queryReservationByUserId(userId).get(t_id) != null) {
            this.paymentService.payTicket(userId, t_id);
            return "pay successfully!";
        }
        return "pay failed!";
    }

    @GetMapping(value = "/payment/cancelPayment/{userId}/{t_id}")
    public String cancelPayment(@PathVariable("userId") Long userId, @PathVariable("t_id") Long t_id) {
        if (paymentService.queryPaymentByUserId(userId).get(t_id) != null) {
            this.paymentService.cancelPayment(userId, t_id);
            return "cancel successfully!";
        }
        return "cancel failed!";

    }


}

